package com.tmt.netty;

import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClient {

	/**
	 * 启动客户端
	 * 
	 * @throws InterruptedException
	 */
	public static void startClient() throws InterruptedException {
		EventLoopGroup group = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
				.handler(new ChannelInitializer<SocketChannel>() {
					@Override
					protected void initChannel(SocketChannel ch) throws Exception {
						 System.out.println("接入到了服务器");
						 ch.pipeline().addLast(new EchoClientHandler());
					}
				});
		ChannelFuture cf = b.connect().sync();
		cf.channel().closeFuture().sync();
		group.shutdownGracefully().sync();
	}
	
	public static void main(String[] args) throws InterruptedException {
		startClient();
	}
}
